$(function() {
	var wait = 60;
	if(localStorage.getItem("checked") == 1) {
		$("#checkbox").prop("checked", true); //很简单就不说了哦
	} else {
		$("#checkbox").prop("checked", false); //很简
	}
	$("#phone").val(localStorage.getItem("gcs_phone"));
	$("#pwd").val(localStorage.getItem("gcs_pwd"));
	if(localStorage.getItem("isLogin") == 1) {
		window.location.href = "home.html";
	}
	history.go(1);
	document.getElementById("smsLabel").addEventListener("click", function() {
		if(!(/^1[3|4|5|8][0-9]\d{4,8}$/.test($("#phoneChange").val()))) {
			alert("手机号不正确");
			return;
		}
		$.ajax({
			//几个参数需要注意一下
			type: "POST", //方法类型
			dataType: "json", //预期服务器返回的数据类型
			url: getCodeUrl, //url
			data: {
				"uid": 123,
				"phone": $("#phoneChange").val()
			},
			success: function(result) {
				if(result.succeed != 0) {
					alert(result.msg);
				} else {
					time();
				}
			},
			error: function() {
				alert("网络异常,请稍候重试！");
			}
		});

	})

	function time() {
		var o = $("#smsLabel");
		if(wait == 0) {
			o.attr("onclick", "sendSms()");
			o.css("color", "#00a5fe");
			o.html("免费获取验证码");
			wait = 60;
		} else {
			o.removeAttr("onclick");
			o.css("color", "grey");
			o.html("重新发送(" + wait + ")");
			wait--;
			setTimeout(function() {
					time(o)
				},
				1000)
		}
	}

	document.getElementById("okchange").addEventListener("click", function() {
		if(!$("#smsCode").val()) {
			alert("请填写验证码");
			return;
		}
		if(!$("#pwdChange").val()) {
			alert("请填写新密码");
			return;
		}
		$.ajax({
			//几个参数需要注意一下
			type: "POST", //方法类型
			dataType: "json", //预期服务器返回的数据类型
			url: forgetPwdUrl, //url
			data: {
				"uid": 123,
				"phone": $("#phoneChange").val(),
				"pwd": $("#pwdChange").val(),
				"code": $("#smsCode").val()
			},
			success: function(result) {
				if(result.succeed != 0) {
					alert(result.msg);
				} else {
					alert("修改成功");
					$("#pwdModal").modal("hide");
				}
			},
			error: function() {
				alert("网络异常,请稍候重试！");
			}
		});

	})
	document.getElementById("change").addEventListener("click", function() {
		$("#pwdModal").modal("show");
	})
	document.getElementById("login").addEventListener("click", function() {
		$.ajax({
			type: "POST", //方法类型
			dataType: "json", //预期服务器返回的数据类型
			url: loginUrl, //url
			data: {
				"phone": $("#phone").val(),
				"pwd": $("#pwd").val()
			},
			success: function(result) {
				if(result.succeed != 0) {
					mui.alert(result.msg);
				} else {
					localStorage.setItem("gcs_user_name", result.data.user.name);
					localStorage.setItem("gcs_user_id", result.data.user.id);
					if($("#checkbox").is(":checked")) {
						localStorage.setItem("checked", "1");
						localStorage.setItem("gcs_phone", $("#phone").val());
						localStorage.setItem("gcs_pwd", $("#pwd").val());
					} else {
						localStorage.setItem("checked", "0");
						localStorage.setItem("gcs_phone", "");
						localStorage.setItem("gcs_pwd", "");
					}
					localStorage.setItem("isLogin", "1");
					window.location.href = "home.html";
				}
			},
			error: function(result) {
				mui.alert("服务器处理失败");
			}
		});
	})
})